home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’92 / PatchWorks Kit / <PatchWorks++> / ShowIconFamily.c < prev    next >
Text File  |  1992-05-30  |  9KB  |  323 lines

  1. /*
  2.     ShowIconFamily.c
  3.     
  4.     ShowINIT compatible routine that shows 'ICN#' and 'iclx' flavor icons.
  5.     For use by all INITs in System 7 and beyond.
  6.     
  7.     by Patrick C. Beard.
  8.     
  9.     Instructions for use:
  10.     
  11.         • Create a family of icons with ResEdit 2.1 or later.  This will include
  12.          'ICN#', 'icl4', & 'icl8' icons.
  13.         • Use SetUpA4 to set up Think C globals.
  14.         • Initialize QuickDraw with InitQD() below.
  15.         • Call ShowIconFamily() with the resource id of the family that you used.
  16.     
  17.     Enhancements:
  18.         
  19.         • Uses 'iclx' & 'ICN#' icons from the Finder's "icon family" in System 7.
  20.         • Generates a position for icons that is guaranteed to be on screen, while
  21.           remaining compatible with previous releases of ShowInit.
  22.     
  23.     This code is completely public domain.  Let's hope this becomes a new standard.
  24.     
  25.     This code is derived from the original ShowInit by Paul Mercer, Darin Adler,
  26.     Paul Snively, and Steve Capps.
  27.     
  28.     Special thanks to Ben Haller & Rob Vaterlaus for valuable suggestions and help.
  29.  */
  30.  
  31. #ifndef __QUICKDRAW__
  32. #include <QuickDraw.h>
  33. #endif
  34. #ifndef __RESOURCES__
  35. #include <Resources.h>
  36. #endif
  37. #ifndef __MEMORY__
  38. #include <Memory.h>
  39. #endif
  40.  
  41. #ifndef nil
  42. #define nil ((void*)0)
  43. #endif
  44. #ifndef topLeft
  45. #define topLeft(r) ((Point*)&r)[0])
  46. #endif
  47. #ifndef botRight
  48. #define botRight(r) ((Point*)&r)[1])
  49. #endif
  50.  
  51. #ifdef THINK_C
  52. #include <QDGlobals.h>
  53. QDGlobals _qd;                                            // the quickdraw globals.
  54. long InitQD(void);
  55. #endif
  56.  
  57. static short theDepth;                                    // the depth the monitor is in.
  58. static CGrafPtr port;                                    // the current port we created.
  59.  
  60. void ShowIconFamily(short iconId);
  61.  
  62. static void DrawBWIcon(short iconId);
  63. static void DrawColorIcon(short iconId);
  64. static void GetIconRect(register Rect* iconRect);
  65. static Handle ChooseIcon(short iconId, short* suggestedDepth);
  66.  
  67. #ifdef THINK_C
  68. long InitQD()
  69. {
  70.     long oldA5 = SetA5((long)&_qd.end);
  71.     InitGraf(&_qd.thePort);
  72.     return oldA5;
  73. }
  74. #endif
  75.  
  76. /* ShowIconFamily(short iconId) -- this is where it all happens. */
  77.  
  78. void ShowIconFamily(short iconId)
  79. {
  80.     SysEnvRec environment;            // machine configuration.
  81.     CGrafPort gp;                    // our grafport.
  82.     
  83.     // find out what kind of machine this is.
  84.     SysEnvirons(curSysEnvVers, &environment);
  85.     if (environment.hasColorQD) {
  86.         theDepth = (**(**GetMainDevice()).gdPMap).pixelSize;
  87.         if (theDepth < 4)
  88.             theDepth = 1;
  89.     } else {
  90.         theDepth = 1;
  91.     }
  92.  
  93.     // open proper type of GrafPort.
  94.     if (theDepth >= 4)
  95.         OpenCPort(&gp);
  96.     else
  97.         OpenPort((GrafPtr)&gp);
  98.     
  99.     port = &gp;
  100.     
  101.     if (theDepth == 1)
  102.         DrawBWIcon(iconId);
  103.     else
  104.         DrawColorIcon(iconId);
  105.     
  106.     if (theDepth >= 4)
  107.         CloseCPort(port);
  108.     else
  109.         ClosePort((GrafPtr)port);
  110. }
  111.  
  112. /*
  113.     ShowInit's information is nestled at the tail end of CurApName.
  114.     It consists of a short which encodes the next horizontal offset,
  115.     and another short which is that value checksummed with the function below.
  116.  */
  117.  
  118. #define CurApName_LM    0x910
  119. #define ShowINITTable ((short*)(CurApName_LM + 32 - 4))
  120. #define CheckSumConst 0x1021        // magic value to check-sum with.
  121.  
  122. #define InitialXPosition 8            // initial horizontal offset.
  123. #define YOffset            40            // constant from bottom to place the icon.
  124. #define XOffset            40            // amount to change it by.
  125.  
  126. // a little bit of nastiness, that can't be done easily in C.
  127. #ifdef __cplusplus
  128. extern "C" {
  129. #endif
  130. #pragma parameter __D0 RolW(__D0)
  131. short RolW(short x) = { 0xE358 };    // rol.w #1, x
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135.  
  136. /* CheckSum() computes the magic value to determine if ShowInit's have run already. */
  137.  
  138. static short CheckSum(register short x)
  139. {
  140.     return (RolW(x) ^ CheckSumConst);
  141. }
  142.  
  143. /*
  144.     GetIconRect() generates an appropriate rectangle to display the
  145.     next INIT's icon in.  It is also responsible for updating the horizontal
  146.     position in low memory.  This is a departure from the original ShowInit code,
  147.     which updates low memory AFTER displaying the icon.  This code won't generate
  148.     an icon position until it is certain that the icon can be loaded, so the
  149.     same behaviour occurs.
  150.     
  151.     This routine also generates a rectangle which is guaranteed to be onscreen.  It
  152.     does this by taking the horizontal offset modulo the screen width to generate
  153.     the horizontal position of the icon, and the offset divided by the screen
  154.     width to generate the proper row.
  155.  */
  156.  
  157. static void GetIconRect(register Rect* iconRect)
  158. {
  159.     register short screenWidth = port->portRect.right - port->portRect.left;
  160.     // if we are the first INIT to run we need to initialize the horizontal value.
  161.     if (CheckSum(ShowINITTable[0]) != ShowINITTable[1])
  162.         ShowINITTable[0] = InitialXPosition;
  163.     
  164.     // compute top left of icon's rect.
  165.     iconRect->left = (ShowINITTable[0] % screenWidth);
  166.     iconRect->top = port->portRect.bottom - YOffset * (1 + (ShowINITTable[0] / screenWidth));
  167.     iconRect->right = iconRect->left + 32;
  168.     iconRect->bottom = iconRect->top + 32;
  169.     
  170.     // advance the position for the next icon.
  171.     ShowINITTable[0] += XOffset;
  172.     
  173.     // recompute the checksum.
  174.     ShowINITTable[1] = CheckSum(ShowINITTable[0]);
  175. }
  176.  
  177. /* DrawBWIcon() draws the 'ICN#' member of the icon family. */
  178.  
  179. static void DrawBWIcon(short iconId)
  180. {
  181.     Handle icon;
  182.     Rect iconRect;
  183.     BitMap source, destination;
  184.     
  185.     icon = Get1Resource('ICN#', iconId);
  186.     if (!icon)
  187.         return;
  188.     HLock(icon);
  189.     
  190.     GetIconRect(&iconRect);
  191.  
  192.     // prepare the source and destination bitmaps.
  193.     source.baseAddr = *icon + 128;                    // mask address.
  194.     source.rowBytes = 4;
  195.     SetRect(&source.bounds, 0, 0, 32, 32);
  196.     destination = ((GrafPtr)port)->portBits;
  197.     
  198.     // transfer the mask.
  199.     CopyBits(&source, &destination, &source.bounds, &iconRect, srcBic, nil);
  200.     
  201.     // and the icon.
  202.     source.baseAddr = *icon;    
  203.     CopyBits(&source, &destination, &source.bounds, &iconRect, srcOr, nil);
  204.     
  205.     ReleaseResource(icon);
  206. }
  207.  
  208. /*
  209.     ChooseIcon() chooses the optimal icon for the current screen depth.
  210.     
  211.     Priorities for choosing icons:
  212.         1. match the bit depth to the icon.
  213.         2. use alternate bit depth version if available.
  214.         3. draw the black & white version.
  215.  */
  216.     
  217. static Handle ChooseIcon(short iconId, short* suggestedDepth)
  218. {
  219.     short depth = *suggestedDepth;
  220.     Handle icon = nil;
  221.  
  222.     if (depth == 4) {
  223.         icon = Get1Resource('icl4', iconId);
  224.         if (!icon) {
  225.             // try alternate depth.
  226.             icon = Get1Resource('icl8', iconId);
  227.             if (icon)
  228.                 depth = 8;
  229.         }
  230.     } else {
  231.         depth = 8;
  232.         icon = Get1Resource('icl8', iconId);
  233.         if (!icon) {
  234.             // try alternate depth.
  235.             icon = Get1Resource('icl4', iconId);
  236.             if (icon)
  237.                 depth = 4;
  238.         }
  239.     }
  240.     
  241.     *suggestedDepth = depth;
  242.     return icon;
  243. }
  244.  
  245. /* DrawColorIcon() draws the appropriate icon for the current screen depth. */
  246.  
  247. static void DrawColorIcon(short iconId)
  248. {
  249.     short depthToUse;
  250.     Handle mask, icon;
  251.     CTabHandle clut;
  252.     PixMapHandle source;
  253.     BitMap maskBits;
  254.     long rowBytes;
  255.     Rect iconRect, bounds;
  256.     
  257.     // by default we will be using the actual depth of the screen.
  258.     depthToUse = theDepth;
  259.     icon = ChooseIcon(iconId, &depthToUse);
  260.     
  261.     // if no color icon available, draw the black & white icon.
  262.     if (!icon) {
  263.         DrawBWIcon(iconId);
  264.         return;
  265.     }
  266.     HLock(icon);
  267.     
  268.     // get the black & white icon to get the mask drawn.
  269.     mask = Get1Resource('ICN#', iconId);
  270.     if (!mask)
  271.         return;
  272.     HLock(mask);
  273.  
  274.     // get the correct color lookup table.
  275.     clut = GetCTable(depthToUse);
  276.     if (!clut)
  277.         return;
  278.     
  279.     // create a pixmap to stick the icon bits into for screen blitting.
  280.     source = NewPixMap();
  281.     if (!source) {
  282.         DisposCTable(clut);
  283.         return;
  284.     }
  285.     
  286.     // set up the source pixmap with the appropriate bounds, depth, and clut.
  287.     bounds.top = bounds.left = 0;
  288.     bounds.bottom = bounds.right = 32;
  289.     rowBytes = (((depthToUse * 32) + 15) / 16) * 2;
  290.     (**source).baseAddr = *icon;
  291.     (**source).rowBytes = ((short)rowBytes) | 0x8000;
  292.     (**source).bounds = bounds;
  293.     (**source).pixelType = 0;            // chunky model.
  294.     (**source).pixelSize = depthToUse;
  295.     (**source).cmpCount = 1;            // if in 32 bit mode this will be 3, so must change.
  296.     (**source).cmpSize = depthToUse;    // only chunky images used.
  297.     DisposCTable((**source).pmTable);    // dispose of default, uninitialized table.
  298.     (**source).pmTable = clut;
  299.  
  300.     // get position to draw icon in.
  301.     GetIconRect(&iconRect);
  302.  
  303.     // prepare the mask bitmap.
  304.     maskBits.baseAddr = *mask + 128;                    // mask address.
  305.     maskBits.rowBytes = 4;
  306.     maskBits.bounds = bounds;
  307.  
  308.     // punch out the mask.    
  309.     CopyBits(&maskBits, (BitMap*)&port->portPixMap, &bounds, &iconRect, srcBic, nil);
  310.     
  311.     // draw the actual color icon.
  312.     HLock((Handle)source);
  313.     CopyBits((BitMap*)*source, (BitMap*)&port->portPixMap, &bounds, &iconRect, srcOr, nil);
  314.     
  315.     // release everything we've allocated.
  316.     (**source).baseAddr = nil;
  317.     DisposPixMap(source);
  318.     
  319.     // release the icon and mask.
  320.     ReleaseResource(icon);
  321.     ReleaseResource(mask);
  322. }
  323.